﻿#pragma once

#include "IIntParser.h"
#include "XBaseObjImpl.h"

template <class T>
IXControl* TIntCreateControl()
{
    T* pControl = NULL;
    if(FAILED(Util::CreateObject(&pControl)))
        return NULL;
    return pControl;
}
typedef IXControl* (*PFN_TIntCreateControl)();

/**
*
*/
class CXIntParserBase : public CXBaseObjImpl<IIntParser>
{
    CXIntParserBase();
    CXIntParserBase(const CXIntParserBase&);
    CXIntParserBase& operator = (const CXIntParserBase&);
public:
    CXIntParserBase(PFN_TIntCreateControl pfnCreator)
    {
        m_pfnCreator = pfnCreator;
    }

    // 创建指针
    virtual IXControl* Alloc()
    {
        IXControl* pControl = m_pfnCreator();
        return pControl;
    }

    // 创建失败时，释放指针
    virtual void    DeAlloc(IN IXControl* pControl)
    {
        XASSERT_ERROR(pControl != NULL, _T("Invalid Parameter, DeAlloc"));
        if(pControl != NULL)
            pControl->Release();
    }

    // 创建控件之前
    virtual BOOL PreCreate(IN IXConfigNode* pNode, IN IXControl* pControl)
    {
        IIntParser* pParentParser = GetParentParser();
        if(pParentParser != NULL)
            return pParentParser->PreCreate(pNode, pControl);
        return TRUE;
    }

    // 将Control添加到控件树中
    virtual BOOL CreateUI(IN IXConfigNode* pNode, IN IXControl* pControl, IN IXControl* pParent)
    {
        IIntParser* pParentParser = GetParentParser();
        if(pParentParser == NULL)
            return TRUE;
        return pParentParser->CreateUI(pNode, pControl, pParent);
    }

    // 填充属性
    virtual BOOL FillAttr(IN IXConfigNode* pNode, IN IXControl* pControl)
    {
        IIntParser* pParentParser = GetParentParser();
        if(pParentParser == NULL)
            return TRUE;
        return pParentParser->FillAttr(pNode, pControl);
    }

    // 创建控件之后
    virtual BOOL PostCreate(IN IXConfigNode* pNode, IN IXControl* pControl)
    {
        IIntParser* pParentParser = GetParentParser();
        if(pParentParser == NULL)
            return TRUE;
        return pParentParser->PostCreate(pNode, pControl);
    }

protected:
    virtual IIntParser* GetParentParser() = 0;

protected:
    IIntParser*             pParentParser;
    PFN_TIntCreateControl   m_pfnCreator;
};
